<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 * 
 */

class TV_Model_DbTable_Tvs extends Engine_Db_Table {

    protected $_rowClass = 'Tv_Model_Tv';

    public function getTvPaginator($params = array()) {
        return Zend_Paginator::factory($this->getTvSelect($params));
    }

    public function getTvSelect($params = array()) {

        if (!empty($params['sort'])) {
            switch ($params['sort']) {
                case 'popular':
                    $order = 'view_count';
                    break;
                case 'recent':
                default:
                    $order = 'modified_date';
                    break;
            }
        } else {
            $order = 'modified_date';
        }

        $select = $this->select()
                ->from(array('t1' => $this->info('name')), '*')
                ->order($order . ' DESC');

        if (!empty($params['search'])) {
            $select->where('title LIKE ? OR description LIKE ?', '%' . $params['search'] . '%');
        }

        if (!empty($params['category_id'])) {
            $select->where('category_id =?', $params['category_id']);
        }
        
        if(!empty($params['country'])){
            $select->where('country =?', $params['country']);
        }
        
        if(!empty($params['language'])){
            $select->where('language =?', $params['language']);
        }

        if (!empty($params['user_id'])) {
            $favouriteTable = Engine_Api::_()->getDbtable('favourites', 'tv');
            $select->joinLeft(array('t2' => $favouriteTable->info('name')), 't2.tv_id = t1.tv_id', null)
                    ->where('t2.user_id =?', $params['user_id']);
        }

        return $select;
    }

    public function getListTv($order) {


        switch ($order) {
            case 'featured':
                $select = $this->select()
                        ->from($this->info('name'), new Zend_Db_Expr('*, RAND()'))
                        ->where('featured =?', 1)
                      //  ->where('status =?', 1)
                        ->limit(1);
                break;
            case 'view':
                $select = $this->select()
                        // ->where('status =?', 1)
                        ->order('view_count DESC');
                break;
            case 'soon':
                $select = $this->select()
                        //   ->where('status =?', 1)
                        ->where('starttime <> ?', '0000-00-00 00:00:00')
                        ->order('starttime ASC');
                break;
            case 'country':
                $select = $this->fetchAll($this->select()
                        ->from($this->info('name'), new Zend_Db_Expr('country, COUNT(*) as count'))
                        //   ->where('status =?', 1)
                        ->where('country <> ?', '')
                        ->order('count DESC')
                        ->group('country')
                        ->limit(20))
                        
                ;  
               break;
               case 'language':
                 $select = $this->fetchAll($this->select()
                        ->from($this->info('name'), new Zend_Db_Expr('language, COUNT(*) as count'))
                        //   ->where('status =?', 1)
                        ->where('language <> ?', '')
                        ->order('count DESC')
                        ->group('language')
                        ->limit(20))
                        
                ;  
        }

        return $select;
    }

}

?>
